package jly521.lucene.queries;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class BooleanQueryTest {

    /**
     * 带权重的查询
     */
	public static void main(String[] args) throws IOException {
        Path indexPath = Paths.get("indexdir");
        Directory dir = FSDirectory.open(indexPath);
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);
        Query query1 = new BoostQuery(new TermQuery(new Term("appName", "金")), 1.0f);
//        Query query2 = new BoostQuery(new TermQuery(new Term("appName", "杰")), 5.0f);
        BooleanClause bc1 = new BooleanClause(query1,Occur.SHOULD);
//        BooleanClause bc2 = new BooleanClause(query2,Occur.SHOULD);
        BooleanQuery boolQuery = new BooleanQuery.Builder()
                .add(bc1)
//                .add(bc2)
                .build();
        System.out.println("Query::" + boolQuery);
        // 返回前10条
        TopDocs tds = searcher.search(boolQuery, 100);
        System.out.println("一共命中：" + tds.totalHits + "条！");
        int idx = 1;
        for (ScoreDoc sd : tds.scoreDocs) {
            System.out.print(idx++ + "：" + sd.doc + ":");
            Document doc = searcher.doc(sd.doc);
            System.out.println("appName:" + doc.get("appName"));
            System.out.println("文档评分:" + sd.score);
            Explanation explanation = searcher.explain(boolQuery, sd.doc);
            System.out.println(explanation.toString());
        }
        dir.close();
        reader.close();
    }

	public static void main11(String[] args) throws IOException {
        Path indexPath = Paths.get("indexdir");
        Directory dir = FSDirectory.open(indexPath);
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);
        Query query1 = new TermQuery(new Term("appName", "金杰"));
        BooleanClause bc1 = new BooleanClause(query1,Occur.SHOULD);
        BooleanQuery boolQuery = new BooleanQuery.Builder().add(bc1).build();
        System.out.println("Query:" + boolQuery);
        // 返回前10条
        TopDocs tds = searcher.search(boolQuery, 100);
        System.out.println("一共命中：" + tds.totalHits + "条！");
        int idx = 1;
        for (ScoreDoc sd : tds.scoreDocs) {
            System.out.print(idx++ + "：");
            Document doc = searcher.doc(sd.doc);
            System.out.println("appName:" + doc.get("appName"));
            System.out.println("文档评分:" + sd.score);
        }
        dir.close();
        reader.close();
    }

	public static void main1(String[] args) throws IOException {
		Path indexPath = Paths.get("indexdir");
		Directory dir = FSDirectory.open(indexPath);
		IndexReader reader = DirectoryReader.open(dir);
		IndexSearcher searcher = new IndexSearcher(reader);
		
		Query query1 = new TermQuery(new Term("title", "特朗普"));
		Query query2 = new TermQuery(new Term("content", "美国"));

		BooleanClause bc1 = new BooleanClause(query1,Occur.SHOULD);
		BooleanClause bc2 = new BooleanClause(query2,Occur.SHOULD);
		BooleanQuery boolQuery = new BooleanQuery.Builder().add(bc1).add(bc2).build();

		System.out.println("Query:" + boolQuery);
		// 返回前10条
		TopDocs tds = searcher.search(boolQuery, 10);
		for (ScoreDoc sd : tds.scoreDocs) {
			// Explanation explanation = searcher.explain(query, sd.doc);
			// System.out.println("explain:" + explanation + "\n");
			Document doc = searcher.doc(sd.doc);
			System.out.println("DocID:" + sd.doc);
			System.out.println("id:" + doc.get("id"));
			System.out.println("title:" + doc.get("title"));
			System.out.println("content:" + doc.get("content"));
			System.out.println("文档评分:" + sd.score);
		}
		dir.close();
		reader.close();

	}

}
